package special_tree;

import code.CodeList;
import code.Opcode;

import compiler.CompilerException;

import lexer.Token;
import tree.DataType;
import tree.MyTree;
import tree.Tree;
import tree.Type;
import tree.TypingException;

public class WhileTree extends MyTree {

	public WhileTree(Token token, Tree cond, Tree body) {
		super(token, cond, body);
	}

	/* (non-Javadoc)
	 * @see tree.MyTree#computeType()
	 */
	@Override
	@Deprecated
	public Type computeType() throws TypingException {
		this.expectType(getOperand(0), new Type(DataType.BOOL,""));
		return getOperand(1).computeType();
	}

	/* (non-Javadoc)
	 * @see tree.MyTree#generateCode(code.CodeList)
	 */
	@Override
	@Deprecated
	public void generateCode(CodeList code) throws CompilerException {
		int start = code.getPosition();
		getOperand(0).generateCode(code);
		int label = code.generateJump(Opcode.JZ);
		getOperand(1).generateCode(code);
		int label2 = code.generateJump(Opcode.JUMP);
		code.patchJump(label2, start);
		code.patchJump(label, code.getPosition());
	}

}
